﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace KeyAssistant
{
    public partial class Form1 : Form
    {
        // ShowWindow参数
        private const int SW_RESTORE = 9;
        public Form1()
        {
            InitializeComponent();
        }

        // 单击
        private void clickBtn_Click(object sender, EventArgs e)
        {
            if (titleTextBox.Text == "")
            {
                MessageBox.Show("请输入窗口标题");
                return;
            }

            if (keyTextBox.Text == "")
            {
                MessageBox.Show("请输入按键");
                return;
            }
            string pattern = @"^[A-Za-z]$";
            Regex regex = new Regex(pattern);
            if (!regex.IsMatch(keyTextBox.Text))
            {
                MessageBox.Show("按键请输入一个英文字母");
                return;
            }

            //IntPtr notepad = FindWindow("Notepad", "product_brand_gouwu_pclady.txt - 记事本");
            IntPtr notepad = FindWindow(null, titleTextBox.Text);
            if (notepad == IntPtr.Zero)
            {
                MessageBox.Show("没有找到窗口");
                return;
            }
            // 将窗口还原
            ShowWindow(notepad, SW_RESTORE);
            // 如果没有ShowWindow，此方法不能设置最小化的窗口
            SetForegroundWindow(notepad);
            SendKeys.SendWait("{" + keyTextBox.Text.ToUpper() + "}");
        }

        // 循环
        private void cycleBtn_Click(object sender, EventArgs e)
        {
            if (titleTextBox.Text == "")
            {
                MessageBox.Show("请输入窗口标题");
                return;
            }
            
            if (keyTextBox.Text == "")
            {
                MessageBox.Show("请输入按键");
                return;
            }
            string pattern = @"^[A-Za-z]$";
            Regex regex = new Regex(pattern);
            if (!regex.IsMatch(keyTextBox.Text))
            {
                MessageBox.Show("按键请输入一个英文字母");
                return;
            }

            if (intervalTime.Value <= 0)
            {
                MessageBox.Show("间隔时间必须大于0");
                return;
            }

            if (intervalTime.Value > 1000)
            {
                MessageBox.Show("间隔时间不能大于1000");
                return;
            }

            if (times.Value <= 0) {
                MessageBox.Show("点击次数必须大于0");
                return;
            }

            if (times.Value > 10000) {
                MessageBox.Show("点击次数不能大于10000");
                return;
            }

            //IntPtr notepad = FindWindow("Notepad", "product_brand_gouwu_pclady.txt - 记事本");
            IntPtr notepad = FindWindow(null, titleTextBox.Text);
            if (notepad == IntPtr.Zero)
            {
                MessageBox.Show("没有找到窗口");
                return;
            }
            // 将窗口还原
            ShowWindow(notepad, SW_RESTORE);
            // 如果没有ShowWindow，此方法不能设置最小化的窗口
            SetForegroundWindow(notepad);
            int i = 0;
            // 循环点击
            while (true) {
                // 超过点击次数退出
                if (i >= times.Value) {
                    break;
                }
                // 当前顶层窗口不是指定窗口则退出
                IntPtr curForeground = GetForegroundWindow();
                if (curForeground != notepad) {
                    break;
                }
                // 向顶层窗口发送点击按键
                SendKeys.SendWait("{" + keyTextBox.Text.ToUpper() + "}");
                // 暂停时间
                Thread.Sleep(Decimal.ToInt32(intervalTime.Value * 1000));
                i++;
            }
            Console.WriteLine("已停止");
        }

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("User32.dll ")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
    }
}
